package cn.test.rabbitmq.normal;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author hanchao
 * @date 2017/6/30 18:24
 */
public class FanoutConsumer {
    private static final String QUEUE_NAME1 = "test-queue-fa1";
    private static final String QUEUE_NAME2 = "test-queue-fa2";
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.1.163");
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("han");
        Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME1, false, false, false, null);
        channel.queueDeclare(QUEUE_NAME2, false, false, false, null);


        channel.setDefaultConsumer(new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body));
            }
        });
        //无确认，会导致消息不删除
        channel.basicConsume(QUEUE_NAME1,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("队列fa1"+new String(body));
            }
        });
        channel.basicConsume(QUEUE_NAME2,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("队列fa2"+new String(body));
            }
        });
    }
}
